package juc;

import org.junit.Test;

public class DanHuanList
{
    private Node figure = new Node(-1);
    public boolean createNodes(int n)
    {
        if (n < 2)
        {
            System.out.println("创建失败！数量不对");
            return false;
        }

        Node temp = null;

        for (int i = 1; i <= n; i++)
        {
            Node node = new Node(i);
            if (i == 1)
            {
                figure = node;
                figure.setNext(figure);
                temp = figure;//让辅助指针指向第一个node
            }
            else
            {
                temp.setNext(node);
                node.setNext(figure);//将当前的指针指向figure
                temp = node;
            }
        }
        System.out.println("创建成功！");
        return true;
    }

    public void showNodes()
    {
        //判断是否为空
        if (figure.getNo() == -1)
        {
            System.out.println("没有任何节点");
            return;
        }
        //因为first不能动，因此仍需要指定一个辅助指针来完成操作
        Node temp = figure;
        while (true)
        {
            System.out.printf("node的编号 %d \n",temp.getNo());
            if (temp.getNext() == figure)//这里说明已经遍历完毕
            {
                break;
            }
            temp = temp.getNext();
        }
    }

    @Test
    public void test()
    {
        DanHuanList danHuanList = new DanHuanList();
        danHuanList.createNodes(1);
        danHuanList.showNodes();
    }
}


class Node
{
    private int no;
    private Node next;

    public Node(int no)
    {
        this.no = no;
    }

    @Override
    public String toString()
    {
        return "Node{" +
                "no=" + no +
                ", next=" + next +
                '}';
    }

    public int getNo()
    {
        return no;
    }

    public void setNo(int no)
    {
        this.no = no;
    }

    public Node getNext()
    {
        return next;
    }

    public void setNext(Node next)
    {
        this.next = next;
    }


}
